package com.moon.game.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

import com.moon.game.constants.Constants;
import com.moon.game.manager.MissionMG;
import com.moon.game.resource.model.Mission;
import com.moon.game.util.Utils;
import com.moon.game.vos.PlayerVo;
import com.moon.game.vos.PlayerVo.MyMission;

/**
 * @description 任务管理
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-8-2 下午4:11:21
 */
public class MissionService {
	private static final int MAX_MISSION_NUM = 5;//个人每天任务最大数量为5个
	private static final int FIX_MISSIONS_SIZE = 15;//适合玩家等级的任务列表大小
	/**
	 * 分派日常任务(可以花钱重新获取未完成的任务)
	 * 
	 * @param player 
	 * @param mode 0表示花钱刷未完成的任务，1表示隔天刷新已经完成的任务
	 */
	public static void assignMission(PlayerVo player, int mode) {
		List<MyMission> myMissions = player.getDailyMissionList();
		List<Mission> fixMissions = new ArrayList<Mission>();
		findFixMissons(player.getLevel(), fixMissions);
		if (myMissions == null) {
			myMissions = new ArrayList<MyMission>();
			//还没分派任务，直接分派5个
			randomGetMission(fixMissions, myMissions, MAX_MISSION_NUM);
			player.setDailyMissionList(myMissions);
		}else {
			//已经完成的列表
			List<MyMission> finishedMissions = new ArrayList<PlayerVo.MyMission>();
			for (MyMission mission : myMissions) {
				if (mission.getStatu() == mode) {
					continue;
				}
				finishedMissions.add(mission);
			}
			
			//分派未完成个新的任务
			randomGetMission(fixMissions, finishedMissions, MAX_MISSION_NUM - finishedMissions.size());
			player.setDailyMissionList(finishedMissions);
		}
		//更新到库
		PlayerService.doUpdatePlayer(player.getSid(), player.getId(), player);
	}
	
	/**
	 * 移除指定类型下的指定任务指定任务
	 * @param player
	 * @param missionId
	 * @param mode 1主线任务，0日常任务
	 */
	public static void removeTheMission(PlayerVo player, Integer missionId) {
		List<MyMission> myMissions = null;
		Mission mission = MissionMG.instance.getMission(missionId);
		if (mission == null) {
			return;
		}
		int mode = mission.getType();
		if (mode == Constants.MAIN_MISSION_TYPE) {
			myMissions = player.getMainMissionList();
		}else if (mode == Constants.DAILY_MISSION_TYPE) {
			myMissions = player.getDailyMissionList();
		}
		if (myMissions == null) {
			return;
		}
		for (MyMission myMission : myMissions) {
			
		}
	}
	
	/**
	 * 随机分配amount个任务
	 * 
	 * @param fixMissions
	 * @param amount
	 */
	private static void randomGetMission(List<Mission> fixMissions, List<MyMission> myMissions, int amount) {
		if (amount > 10 || amount < 1) {
			//人物数据保护，数量不能超过界定值
			return;
		}
		List<Integer> hasAssignedMission = new ArrayList<Integer>();
		if (myMissions != null && myMissions.size() > 0) {
			for (MyMission mission : myMissions) {
				hasAssignedMission.add(mission.getMissionId());
			}
		}
		int i = 0;
		int k = 0;//保护循环50次循环保护
		while (i < amount && k < 50) {
			k ++;
			int missionId = fixMissions.get(new Random().nextInt(fixMissions.size())).getId();
			if (missionId == 0 || hasAssignedMission.contains(new Integer(missionId))) {
				continue;
			}
			i++;
			hasAssignedMission.add(missionId);
			myMissions.add(MyMissionFactory(missionId));
		}
	}
	
	/**
	 * 任务工厂
	 * 
	 * @param missionId
	 * @return
	 */
	public static MyMission MyMissionFactory(int missionId) {
		MyMission mission = new MyMission();
		mission.setMissionId(missionId);
		mission.setStatu(0);
		return mission;
	}
	
	/**
	 * 取出合适的任务列表
	 * 
	 * @param level
	 */
	private static void findFixMissons(int level, List<Mission> fixMissions) {
		if (level < 1) {
			return;
		}
		List<Mission> levelMissionList = MissionMG.instance.getLevelMissionList(level);
		if (fixMissions.size() < FIX_MISSIONS_SIZE) {
			if (levelMissionList == null) {
				findFixMissons(level - 1, fixMissions);
				return;
			} else {
				for (Mission mission : levelMissionList) {
					fixMissions.add(mission);
				}
			}
		}else {
			findFixMissons(level - 1, fixMissions);
			return;
		}
	}
	
	
	/**
	 * 解析任务
	 * 
	 * @param mission
	 * @param itemMap 收集填充的道具<id,num>
	 * @param monsterMap 打怪填充的怪物<id,num>
	 */
	public static void analyzeMission(Mission mission, HashMap<Integer, Integer> itemMap, HashMap<Integer, Integer> monsterMap) {
		String itemStandard = mission.getItemNum();
		if (itemStandard != null && itemStandard.length() > 0) {
			itemMap.putAll(Utils.analyzeString(itemStandard));
		}
		String monsterStandard = mission.getMonsterNum();
		if (monsterStandard != null && monsterStandard.length() > 0) {
			monsterMap.putAll(Utils.analyzeString(monsterStandard));
		}
	}
	
	
	/**
	 * 完成任务奖励
	 * @param player
	 * @param mission
	 */
	public static void doReward(PlayerVo player, Mission mission) {
		Integer rewardGold = mission.getRewardGold();
		Integer rewardMoney = mission.getRewardMoney();
		Integer rewardExp = mission.getRewardExp();
		HashMap<Integer, Integer> rewardItemMap = null;
		String rewardItems = mission.getRewardItemsNums();
		if (rewardItems != null && rewardItems.length() > 0) {
			rewardItemMap = new HashMap<Integer, Integer>();
			HashMap<Integer, Integer> addItems = Utils.analyzeString(rewardItems);
			if (addItems != null) {
				rewardItemMap.putAll(addItems);
			}
		}
		
		if (rewardGold != null && rewardGold.intValue() > 0) {
			//加金币
			PlayerService.doAddGold(player, rewardGold);
		}
		
		if (rewardMoney != null && rewardMoney.intValue() > 0) {
			//加碎银
			PlayerService.doAddMoney(player, rewardMoney);
		}
		
		if (rewardExp != null && rewardExp.intValue() > 0) {
			//加经验
			PlayerService.doAddExp(player.getSid(), player.getId(), rewardExp);
		}
		
		if (rewardItemMap != null && rewardItemMap.size() > 0) {
			//加道具
			ItemService.addItems(player, new ArrayList<Integer>(rewardItemMap.keySet()), new ArrayList<Integer>(rewardItemMap.values()), false);
		}
	}
	
	
	/**
	 * 更新打怪任务
	 * 
	 * @param player
	 * @param monsterId
	 */
	public static void doHuntMonsterMission(PlayerVo player, int monsterId) {
		List<MyMission> myMissions = player.getDailyMissionList();
		if (myMissions == null) {
			return;
		}
		
		for (MyMission myMission : myMissions) {
			Mission mission = MissionMG.instance.getMission(myMission.getMissionId());
			if (mission == null) {
				//找不到该任务
				return;
			}
			
			String requireMonster = mission.getMonsterNum();
			if (requireMonster == null || requireMonster.length() == 0) {
				//此任务不需要打怪
				return;
			}
			
			HashMap<Integer, Integer> requireMap = Utils.analyzeString(requireMonster);
			if (requireMap.containsKey(monsterId)) {
				HashMap<Integer, Integer> process = myMission.getMonsterProcess();
				if (process != null) {
					if (process.containsKey(monsterId)) {
						//已经开始进行了
						process.put(monsterId, process.get(monsterId) + 1);
					}else {
						//还没有开始进行此怪物的捕获
						process.put(monsterId, 1);
					}
				}else {
					//还没有开始进行任何怪物的捕获
					process = new HashMap<Integer, Integer>();
					process.put(monsterId, 1);
				}
				myMission.setMonsterProcess(process);
				PlayerService.doUpdatePlayer(player.getSid(), player.getId(), player);
			}
		}
	}
	
	
	/**
	 * 查找完成指定任务的后续的任务列表
	 * 
	 * @param missionId
	 * @return
	 */
	public static List<Integer> findNextMission(Integer missionId) {
		List<Integer> nextMissions = new ArrayList<Integer>();
		Mission mission = MissionMG.instance.getMission(missionId);
		if (mission != null) {
			String nextMissionIds = mission.getNextIds();
			if (nextMissionIds != null && nextMissionIds.length() > 0) {
				nextMissions.addAll(Utils.analyzeSimpleString(nextMissionIds));
			}
		}
		return nextMissions;
	}
}
